#!/usr/bin/python2.4
#
#

__author__ = 'Daniel Wilcox <dmwilcox@gmail.com>'

import logging
import yaml


def GetYamlDictFromFile(filename):
  """Read a .yaml file and return a dictionary or None."""
  try:
    yaml_data = yaml.load(open(filename).read())
  except IOError:
    yaml_data = ''
  return yaml_data or None


def ObjAttrsToDict(pref_obj, keys):
  pref_dict = {}
  for key in keys:
    try:
      value = getattr(pref_obj, key)
    except:
      continue
    pref_dict[key] = value
  return pref_dict


def MergeDicts(base_dict, new_dict):
  """Merge dictionaries, new onto base."""
  merged_dict = base_dict.copy()
  for key, value in new_dict.items():
    if value is not None:
      merged_dict[key] = value
  return merged_dict 

 
def setupModuleLogger(module_name):
  module_logger = logging.getLogger(module_name)
  module_logger.setLevel(logging.DEBUG)
  console_handler = logging.StreamHandler()
  console_handler.setLevel(logging.DEBUG)
  formatter = logging.Formatter(('%(asctime)s - %(name)s - %(levelname)s -'
                                ' %(message)s'))
  console_handler.setFormatter(formatter)
  module_logger.addHandler(console_handler)
  return module_logger
